tabs.js ➔ gotoTab   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 16
rs 9.3333
c 0
b 0
f 0
cc 5
1
define(function () {
2
  'use strict';
3
4
  return function () {
5
    var self = this;
6
7
    var tabs = document.createElement('ul');
8
    tabs.classList.add('tabs');
9
10
    var container = document.createElement('div');
11
12
    function gotoTab(li) {
13
      for (var i = 0; i < tabs.children.length; i++) {
14
        tabs.children[i].classList.remove('visible');
15
      }
16
17
      while (container.firstChild) {
18
        container.removeChild(container.firstChild);
19
      }
20
21
      li.classList.add('visible');
22
23
      var tab = document.createElement('div');
24
      tab.classList.add('tab');
25
      container.appendChild(tab);
26
      li.child.render(tab);
27
    }
28
29
    function switchTab() {
30
      gotoTab(this);
31
32
      return false;
33
    }
34
35
    self.add = function add(title, d) {
36
      var li = document.createElement('li');
37
      li.textContent = _.t(title);
38
      li.onclick = switchTab;
39
      li.child = d;
40
      tabs.appendChild(li);
41
42
      var anyVisible = false;
43
44
      for (var i = 0; i < tabs.children.length; i++) {
45
        if (tabs.children[i].classList.contains('visible')) {
46
          anyVisible = true;
47
          break;
48
        }
49
      }
50
51
      if (!anyVisible) {
52
        gotoTab(li);
53
      }
54
    };
55
56
    self.render = function render(el) {
57
      el.appendChild(tabs);
58
      el.appendChild(container);
59
    };
60
61
    return self;
62
  };
63
});
64